var BaseTutorUcenter = app.controller('BaseTutorUcenter', function($rootScope, $scope, $http,$state,$cordovaCamera,$cordovaFileTransfer,$cordovaProgress,$cordovaChat,
		BaseTutorUcenterData) {
	try{
		$rootScope.showf=true;
		$rootScope.title = '用户中心';
		$rootScope.tab = 4;
		$scope.show=false;

		bannner_height("consultant_header",510/720);
		//获取用户信息
		$scope.user={};
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=userinfo";
		$http.jsonp(url).success(function(data) {
			$scope.user=data.retval;
		});

		//退出登录
		$scope.logout = function(e){
			if(e){
				e.stopPropagation();
			}
			try{
				$rootScope.istutor = false;
				var url=SITE_URL+"index.php?app=usermyapp&act=logout&callback=JSON_CALLBACK";
				$http.jsonp(url).success( function(data) {
					if(data.done){
						var reg = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g ;  
						if(!reg.test(window.localStorage.u)){  
							window.localStorage.u="";
						} 

						window.localStorage.p="";
						window.localStorage.user="";


						if ($cordovaChat.ChatConnected()) {
							//$cordovaChat.ChatDisConnect();
						}

						$rootScope.go("loginmain");
					}
				});
			}
			catch(e){showcatch(e);}
		};

		//------------------------用户修改头像
		//拍照
		$scope.usertakephoto=function(e){

			if(e){
				e.stopPropagation();
			}
			var act="upheaderimg";
			$cordovaCamera.getPicture({ quality: 100,
				destinationType: Camera.DestinationType.FILE_URI,
				targetWidth: 1024,
				targetHeight: 1024,
				allowEdit:false,
				sourceType:1,
				Direction:1
			}).then(function (res) {
				console.log(res);
				plugins.crop(function (imageURI) {
					if(imageURI==null||imageURI==""){
						return;
					}
					var url = $rootScope.site + "index.php?app=usermyapp&act=upfile&callback=JSON_CALLBACK";
					$http.jsonp(url).success(function (data) {
						console.log(data);
						if (data.done) {
							$rootScope.ECM_ID = data.retval.userid;
							$rootScope.HTTP_USER_AGENT = data.retval.HTTP_USER_AGENT;
							var uri = $rootScope.site + 'index.php?app=appupload&ajax=ajax&XDEBUG_SESSION_START=ECLIPSE_DBGP';
							console.log(uri);
							var options = new FileUploadOptions();
							options.fileKey = "file";
							options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
							options.mimeType = "text/plain";
							var params = {};
							params.ECM_ID = $rootScope.ECM_ID;
							params.HTTP_USER_AGENT = $rootScope.HTTP_USER_AGENT;
							params.belong = 5;
							params.item_id = $scope.user.user_id;
							options.params = params;
							ProgressIndicator.showProgressBar('正在上传...');
							$cordovaFileTransfer.upload(uri, imageURI, options).then(function (r) {
								console.log("Code = " + r.responseCode);
								console.log("Response = " + r.response);
								console.log("Sent = " + r.bytesSent);
								ProgressIndicator.hide();
								data = JSON.parse(r.response);
								if (data.done) {
									$scope.user.headimgurl = data.retval.file_path;
									console.log("sender.src = " + $scope.user.headimgurl);
									url = $rootScope.site + "index.php?app=usermyapp&act="+act+"&headerimg=" + data.retval.file_path + "&callback=JSON_CALLBACK";
									$http.jsonp(url).success(function (data) {
										showtoast('上传成功');
										$scope.show = false;
									});
								}
								else {
									showtoast(data.msg);
								}
							}, function (error) {
								console.log("An error has occurred: Code = " + error.code);
								console.log("upload error source " + error.source);
								console.log("upload error target " + error.target);
								ProgressIndicator.hide();
								showtoast('图片选择失败，' + error);
								$scope.show = false;
							}, function (progressEvent) {
								ProgressIndicator.setProgress(progressEvent.loaded / progressEvent.total);
							});
						}
					});
				}, function (e) {
				}, res, {width: 300, height: 300, quality: 80});
			});

		}

		//本地图片
		$scope.userlocalpic=function(e){
			if(e){
				e.stopPropagation();
			}
			window.imagePicker.getPictures(
					function(results) {
						plugins.crop(function (newpath) {
							if(newpath==null||newpath==""){
								return;
							}
							var url=$rootScope.site+"index.php?app=usermyapp&act=upfile&callback=JSON_CALLBACK";

							$http.jsonp(url).success( function(data) {
								console.log(data);
								if(data.done){
									for (var i = 0; i < results.length; i++) {
										var imageURI=newpath;
										$rootScope.ECM_ID=data.retval.userid;
										$rootScope.HTTP_USER_AGENT = data.retval.HTTP_USER_AGENT;

										var uri = $rootScope.site+'index.php?app=appupload&ajax=ajax&XDEBUG_SESSION_START=ECLIPSE_DBGP';

										var options = new FileUploadOptions();
										options.fileKey="file";
										options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
										options.mimeType="text/plain";

										var params = {};
										params.ECM_ID = $rootScope.ECM_ID;
										params.HTTP_USER_AGENT = $rootScope.HTTP_USER_AGENT;
										params.belong = 5;
										params.item_id = $scope.user.user_id;
										options.params = params;

										ProgressIndicator.showProgressBar('正在上传...');
										$cordovaFileTransfer.upload(uri,imageURI,options).then(function (r) {
											console.log("Code = " + r.responseCode);
											console.log("Response = " + r.response);
											console.log("Sent = " + r.bytesSent);
											ProgressIndicator.hide();
											data = JSON.parse(r.response);
											if(data.done){
												$scope.user.headimgurl=data.retval.file_path;
												console.log("sender.src = " + $scope.user.headimgurl);
												url=$rootScope.site+"index.php?app=usermyapp&act=uptutorheaderimg&headerimg="+data.retval.file_path+"&callback=JSON_CALLBACK";
												$http.jsonp(url).success( function(data) {
													showtoast('上传成功');
													$scope.show=false;
												});
											}
											else{
												showtoast(data.msg);
											}
										}, function (error) {
											console.log("An error has occurred: Code = " + error.code);
											console.log("upload error source " + error.source);
											console.log("upload error target " + error.target);
											ProgressIndicator.hide();
											showtoast('图片选择失败，'+error);
											$scope.show=false;
										}, function(progressEvent) {
											ProgressIndicator.setProgress(progressEvent.loaded / progressEvent.total);
										});
									}
								}
							});
						}, function (e) {
						}, results[0], {width: 300, height: 300, quality: 80});
					}, function (error) {
						console.log('Error: ' + error);
					}, {
						maximumImagesCount: 1,
						width: 1024,
						height: 1024
					}
			);
		}

		//------------------------导师修改背景图
		//拍照
		$scope.takephoto=function(e){

			if(e){
				e.stopPropagation();
			}
			var act="uptutorheaderimg";
			$cordovaCamera.getPicture({ quality: 100,
				destinationType: Camera.DestinationType.FILE_URI,
				targetWidth: 1024,
				targetHeight: 1024,
				allowEdit:false,
				sourceType:1,
				Direction:1
			}).then(function (res) {
				console.log(res);
				plugins.crop(function (imageURI) {
					if(imageURI==null||imageURI==""){
						return;
					}
					var url = $rootScope.site + "index.php?app=usermyapp&act=upfile&callback=JSON_CALLBACK";
					$http.jsonp(url).success(function (data) {
						console.log(data);
						if (data.done) {
							$rootScope.ECM_ID = data.retval.userid;
							$rootScope.HTTP_USER_AGENT = data.retval.HTTP_USER_AGENT;
							var uri = $rootScope.site + 'index.php?app=appupload&ajax=ajax&XDEBUG_SESSION_START=ECLIPSE_DBGP';
							console.log(uri);
							var options = new FileUploadOptions();
							options.fileKey = "file";
							options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
							options.mimeType = "text/plain";
							var params = {};
							params.ECM_ID = $rootScope.ECM_ID;
							params.HTTP_USER_AGENT = $rootScope.HTTP_USER_AGENT;
							params.belong = 5;
							params.item_id = $scope.user.user_id;
							options.params = params;
							ProgressIndicator.showProgressBar('正在上传...');
							$cordovaFileTransfer.upload(uri, imageURI, options).then(function (r) {
								console.log("Code = " + r.responseCode);
								console.log("Response = " + r.response);
								console.log("Sent = " + r.bytesSent);
								ProgressIndicator.hide();
								data = JSON.parse(r.response);
								if (data.done) {
									$scope.user.tutor_head = data.retval.file_path;
									console.log("sender.src = " + $scope.user.tutor_head);
									url = $rootScope.site + "index.php?app=usermyapp&act="+act+"&headerimg=" + data.retval.file_path + "&callback=JSON_CALLBACK";
									$http.jsonp(url).success(function (data) {
										showtoast('上传成功');
										$scope.show = false;
									});
								}
								else {
									showtoast(data.msg);
								}
							}, function (error) {
								console.log("An error has occurred: Code = " + error.code);
								console.log("upload error source " + error.source);
								console.log("upload error target " + error.target);
								ProgressIndicator.hide();
								showtoast('图片选择失败，' + error);
								$scope.show = false;
							}, function (progressEvent) {
								ProgressIndicator.setProgress(progressEvent.loaded / progressEvent.total);
							});
						}
					});
				}, function (e) {
				}, res, {width: 720, height: 510, quality: 80});
			});

		}

		//本地图片
		$scope.localpic=function(e){
			if(e){
				e.stopPropagation();
			}
			window.imagePicker.getPictures(
					function(results) {
						plugins.crop(function (newpath) {
							if(newpath==null||newpath==""){
								return;
							}
							var url=$rootScope.site+"index.php?app=usermyapp&act=upfile&callback=JSON_CALLBACK";

							$http.jsonp(url).success( function(data) {
								console.log(data);
								if(data.done){
									for (var i = 0; i < results.length; i++) {
										var imageURI=newpath;
										$rootScope.ECM_ID=data.retval.userid;
										$rootScope.HTTP_USER_AGENT = data.retval.HTTP_USER_AGENT;

										var uri = $rootScope.site+'index.php?app=appupload&ajax=ajax&XDEBUG_SESSION_START=ECLIPSE_DBGP';

										var options = new FileUploadOptions();
										options.fileKey="file";
										options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
										options.mimeType="text/plain";

										var params = {};
										params.ECM_ID = $rootScope.ECM_ID;
										params.HTTP_USER_AGENT = $rootScope.HTTP_USER_AGENT;
										params.belong = 5;
										params.item_id = $scope.user.user_id;
										options.params = params;

										ProgressIndicator.showProgressBar('正在上传...');
										$cordovaFileTransfer.upload(uri,imageURI,options).then(function (r) {
											console.log("Code = " + r.responseCode);
											console.log("Response = " + r.response);
											console.log("Sent = " + r.bytesSent);
											ProgressIndicator.hide();
											data = JSON.parse(r.response);
											if(data.done){
												$scope.user.tutor_head=data.retval.file_path;
												console.log("sender.src = " + $scope.user.headimgurl);
												url=$rootScope.site+"index.php?app=usermyapp&act=uptutorheaderimg&headerimg="+data.retval.file_path+"&callback=JSON_CALLBACK";
												$http.jsonp(url).success( function(data) {
													showtoast('上传成功');
													$scope.show=false;
												});
											}
											else{
												showtoast(data.msg);
											}
										}, function (error) {
											console.log("An error has occurred: Code = " + error.code);
											console.log("upload error source " + error.source);
											console.log("upload error target " + error.target);
											ProgressIndicator.hide();
											showtoast('图片选择失败，'+error);
											$scope.show=false;
										}, function(progressEvent) {
											ProgressIndicator.setProgress(progressEvent.loaded / progressEvent.total);
										});
									}
								}
							});
						}, function (e) {
						}, results[0], {width: 720, height: 510, quality: 80});
					}, function (error) {
						console.log('Error: ' + error);
					}, {
						maximumImagesCount: 1,
						width: 1024,
						height: 1024
					}
			);
		}


		//头像
		$scope.headimgurl = function(){
			$scope.show=true;
		};
	}
	catch (e) {
		showcatch(e);
	}
});


BaseTutorUcenter.BaseTutorUcenterData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}

		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}